﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Imaginecup.sprite;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.shareData;

namespace Imaginecup.util.rainEffect
{
    public class RainGenerator
    {
        private int m_Maximum = 50;
        private int m_MaxRain = 10;
        private ParticleMan m_ParticleMan;

        private double m_MaxRainDelay = 0.2;
        private double m_CurrentDelayTime = 0;

        public RainGenerator()
        {
            m_ParticleMan = ParticleMan.GetParticleMan();
        }

        public  void GeneratorRain(GameTime gameTime)
        {
            for (int i = m_ParticleMan.GetCount(ParticleType.Rain); i < m_MaxRain; i++)
            {
                m_ParticleMan.AddParticle(CreateRain());      
            }

            m_CurrentDelayTime += gameTime.ElapsedGameTime.TotalSeconds;
            if (m_CurrentDelayTime > m_MaxRainDelay )
            {
                m_CurrentDelayTime -= m_MaxRainDelay;

                m_MaxRain += 3;
                if (m_MaxRain > m_Maximum)
                {
                    m_MaxRain = m_Maximum;
                }
            }

            
        }

        private Particle CreateRain()
        {
            Vector2 currentPos = new Vector2(RandomUtil.rand.Next(800), 150);
            Vector2 size = new Vector2();
            size.Y = RandomUtil.rand.Next(10, 30) + 5;
            size.X = size.Y * 0.8f;

            int velocity = RandomUtil.rand.Next(60, 80) + 10;
            int endDistance = RandomUtil.rand.Next(350, 480);

            double existTime = (endDistance - currentPos.Y) / velocity;

            Vector2 dir = new Vector2(0, 1);

            Particle particle = new Particle(currentPos, Color.White, existTime, size, dir, velocity , ParticleType.Rain);

            ParticleSprite particleSprite = new ParticleSprite();
            particleSprite.SetColorClass(particle);
            particleSprite.SetTextureName("Rain");

            particle.SetSprite(particleSprite);

            return particle;
        }
    }
}
